package com.ruiyu.IM;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ruiyu.IM.domain.MyApp;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

/**
 * Created by 瑞玉 on 2016/1/14.
 */
public class LoginActivity extends Activity {

    private EditText mAccount;
    private EditText mPassword;
    private Button mLogin;
    private XMPPConnection conn;
    private String username;
    private String password;
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //创建链接通道
                    ConnectionConfiguration configuration = new ConnectionConfiguration(MyApp.HOST, MyApp.PORT);
                    conn = new XMPPConnection(configuration);
                    // Debug信息
                    configuration.setDebuggerEnabled(true);
                    // 使用明文
                    configuration.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);//
                    conn.connect();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }

            }
        }).start();
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = mAccount.getText().toString().trim();
                password = mPassword.getText().toString().trim();
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            conn.login(username,password);
                            flag = true;
                        } catch (XMPPException e) {
                            e.printStackTrace();
                            flag = false;
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (flag){
                                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                                    MyApp.conn = conn;
                                    MyApp.username = username;
                                    MyApp.account = username + "@" + MyApp.SERVICE_NAME;
                                    finish();
                                }else{
                                    Toast.makeText(LoginActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }.start();
            }
        });
    }

    private void initView() {
        mAccount = (EditText) findViewById(R.id.et_account);
        mPassword = (EditText) findViewById(R.id.et_password);
        mLogin = (Button) findViewById(R.id.btn_login);

    }


}
